package com.icbc.errors.adapter;

import com.icbc.errors.HttpError;
import org.springframework.lang.NonNull;

import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

public class DefaultHttpErrorAttributesAdapter implements HttpErrorAttributesAdapter {

    @Override
    @NonNull
    public Map<String, Object> adapt(@NonNull HttpError httpError) {
        return httpError.getErrors().stream()
                .map(this::toMap)
                .collect(collectingAndThen(
                        toList(),
                        errors -> singletonMap("errors", errors)
                ));
    }

    private Map<String, String> toMap(HttpError.CodedMessage codedMessage) {
        Map<String, String> map = new HashMap<>();
        map.put("code", codedMessage.getCode());
        map.put("message", codedMessage.getMessage());

        return map;
    }
}
